home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 262_01.zip / MENU.C < prev    next >
Text File  |  1993-04-14  |  3KB  |  104 lines

  1. /********************************************************
  2.  *                      M E N U                         *
  3.  *                                                      *
  4.  *                  by Robert Ramey                     *
  5.  *                      May 1987                        *
  6.  ********************************************************/
  7.  
  8. #include "stdio.h"
  9.  
  10. struct menu {
  11.     char *question;
  12.     struct mline {
  13.         char *answer;
  14.         int (*routine)();
  15.         int *parameter;
  16.     }
  17.     selection[1];
  18. };
  19.  
  20. #define MENU    struct menu
  21. #define MLINE   struct mline
  22.  
  23. #define MAXMDEPTH 8  /* maximum depth of menus */
  24. char hist[MAXMDEPTH],    /* sequence of responses */
  25.     *hptr = hist;   /* pointer to next available spot */
  26.  
  27. /****************************************************************
  28. menu - a function which manages dialog according to a hierarchy
  29. of menus.  The address of the root menu must be passed as the only
  30. parameter.
  31. *****************************************************************/
  32. int menu(maddress)
  33. MENU maddress[];
  34. {
  35.     char response,*lptr;
  36.     int i, j;
  37.  
  38.     /* repeat menu until operator response is RETURN or ESCAPE */
  39.     /* or an action routine returns a non-zero value */
  40.     for(;;){
  41.         /* display the menu header */
  42.         fputs(maddress->question,stderr);
  43.         putc('\n', stderr);
  44.  
  45.         /* display each line in the menu */
  46.         for(i = 0;lptr = maddress->selection[i].answer;++i){
  47.             fputs(lptr,stderr);
  48.             putc('\n',stderr);
  49.         }
  50.  
  51.         /* loop until a valid response is recieved */
  52.         for(;;){
  53.             putc(':',stderr);   /* display prompt */
  54.             response = getchar();   /* and get response */
  55.             putc('\n',stderr);
  56.  
  57.             switch(response){
  58.             case EOF:
  59.             case ESC:
  60.                 return 99;  /* go back 99 levels */
  61.             case NEWLINE:
  62.                 return 0;   /* repeat prev menu */
  63.             }
  64.  
  65.  
  66.             /* check response against menu lines */
  67.             /* a valid response matches the first */
  68.             /* character of a menu line */
  69.             response = toupper(response);
  70.             for(j = i ; response !=
  71.             toupper(maddress->selection[j].answer[0])
  72.             && j-- > 0;);
  73.             /* check response against menu entries */
  74.  
  75.             if(j < 0)
  76.                 fputs(
  77.                 "invalid response. Try again.\n"
  78.                 ,stderr);
  79.             else
  80.                 break;
  81.         }
  82.  
  83.         /* update hist string to record the sequence of */
  84.         /* responses that led us to this point in the menus */
  85.         *hptr++ = response;
  86.         *hptr = NULL;
  87.  
  88.         /* call the action routine which corresponds to the */
  89.         /* selected menu line */
  90.         response = (*(maddress->selection[j].routine))
  91.             (maddress->selection[j].parameter);
  92.  
  93.         /* restore hist string */
  94.         *--hptr = NULL;
  95.  
  96.         /* undo menu calls until returned value is 0 */
  97.         if(response)
  98.             return response - 1;
  99.  
  100.     }
  101. }
  102.  
  103.  
  104.      /* responses that led us to this point in the menus */